package com.siwei.mes.controller.experiment;

import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.validator.AddGroup;
import com.siwei.mes.common.validator.PageGroup;
import com.siwei.mes.common.validator.UpdateGroup;
import com.siwei.mes.entity.experiment.Experiment;
import com.siwei.mes.entity.experiment.ExperimentSampleTake;
import com.siwei.mes.service.experiment.ExperimentSampleTakeService;
import com.siwei.mes.service.laboratory.UploadLaboratoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 试验台账取样相关
 *
 * @author linzi
 * @since 2023-12-28 20:47:32
 */
@Slf4j
@Api(value = "试验台账取样")
@RestController
@RequestMapping("experimentSampleTake")
public class ExperimentSampleTakeController {
    @Resource
    private ExperimentSampleTakeService experimentSampleTakeService;
    @Resource
    private UploadLaboratoryService uploadLaboratoryService;

    @ApiOperation(value = "新增试验台账取样")
    @PostMapping("/create")
    public Json create(@RequestBody @Validated(AddGroup.class) ExperimentSampleTake entity) {
        return Json.succ().data(ApiResult.addColumn("id", experimentSampleTakeService.create(entity)));
    }

    @ApiOperation(value = "修改试验台账取样")
    @PostMapping("/update")
    public Json update(@RequestBody @Validated(UpdateGroup.class) ExperimentSampleTake entity) {
        experimentSampleTakeService.update(entity);
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "删除试验台账取样")
    @PostMapping("/delete")
    public Json delete(@RequestBody ExperimentSampleTake entity) {
        experimentSampleTakeService.delete(entity.getId());
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "分页获取试验台账取样")
    @PostMapping("/page")
    public Json appPage(@RequestBody @Validated(PageGroup.class) PageQuery<ExperimentSampleTake, ExperimentSampleTake> query) {
        return Json.succ().data(experimentSampleTakeService.getPage(query));
    }

    @ApiOperation(value = "根据主键id获取试验台账取样")
    @GetMapping("loadById")
    public Json loadById(@RequestParam("id") Long id) {
        return Json.succ().data(experimentSampleTakeService.loadById(id));
    }

    @ApiOperation(value = "获取所有试验台账取样")
    @PostMapping("getAll")
    public Json getAll(@RequestBody ExperimentSampleTake entity) {
        return Json.succ().data(ApiResult.addList(experimentSampleTakeService.getAll(entity)));
    }

    @ApiOperation(value = "新增或修改根据试验台账")
    @PostMapping("/insertOrUpdateByExperimentId")
    public Json insertOrUpdateByExperimentId(@RequestBody @Validated(AddGroup.class) ExperimentSampleTake entity) {
        Experiment experiment = experimentSampleTakeService.insertOrUpdateByExperimentId(entity);
    /*    if (experiment != null) {
            if (!MaterialsEnum.CONCRETE.getCode().equals(experiment.getExperimentType())) {
                try {
                    uploadLaboratoryService.generateLedger(new UploadLaboratoryEntity().setExptId(experiment.getId()));
                    uploadLaboratoryService.samplingOperation(new UploadLaboratoryEntity().setExptId(experiment.getId()));
                } catch (Exception e) {
                    log.error("试验台账ID：【{}】保存取样调用协会接口报错【{}】", experiment.getId(), e);
                }
            }
        }*/
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "根据试验台账获取取样记录")
    @GetMapping("selectByExperimentId")
    public Json selectByExperimentId(@RequestParam("experimentId") Long experimentId) {
        return Json.succ().data(experimentSampleTakeService.selectByExperimentId(experimentId));
    }
}
